package com.qumitech.visualnovel.controller.rest;


import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.pubdef.dto.union.*;
import com.qumitech.visualnovel.service.union.UnionService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/api/union")
public class UnionAdminController {

    @Autowired
    private UnionService unionService;

    @PostMapping("")
    public BaseRsp saveUnionConfig(@RequestBody UnionConfigSaveAdminDTO saveAdminDTO) {
        return unionService.saveUnionConfig(saveAdminDTO);
    }

    @PutMapping("/{union_id}")
    public BaseRsp updateUnionConfig(@PathVariable("union_id") String unionId, @RequestBody UnionConfigSaveAdminDTO updateAdminDTO){
        unionService.updateUnionConfig(unionId, updateAdminDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("")
    public BaseRsp listUnionConfig(UnionConfigQueryAdminDTO queryAdminDTO){
        return unionService.listUnionConfig(queryAdminDTO);
    }

    @PostMapping("/user")
    public BaseRsp saveUnionUserInfo(@RequestBody UnionUserInfoSaveAdminDTO saveAdminDTO) {
        unionService.saveUnionUserInfo(saveAdminDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/user/{uid}")
    public BaseRsp rmUnionUserInfo(@PathVariable("uid") Integer uid)
    {
        unionService.rmUnionUserInfo(uid);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/user/{uid}")
    public BaseRsp updateUserInfo(@PathVariable("uid") Integer uid,
                                  @RequestBody UnionUserInfoUpdateDTO updateDTO){
        unionService.updateUserInfo(uid, updateDTO.getPassword(), updateDTO.getDeleteFlag());
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/user")
    public BaseRsp listUnionUserInfo(UnionUserInfoQueryAdminDTO queryAdminDTO){
        if (queryAdminDTO.getUnionName() != null) {
            String unionId = unionService.findUnionIdByUnionName(queryAdminDTO);
            if (unionId == null){
                ImmutableMap immutableMap = ImmutableMap.of("union_user",new ArrayList<>(),"count",0);
                return new BaseRsp(ResultEnum.SUCCESS, immutableMap);
            }
            else{
                queryAdminDTO.setUnionId(unionId);
            }
        }
        ImmutableMap immutableMap = ImmutableMap.of("union_user",unionService.listUnionUserInfo(queryAdminDTO),"count",unionService.countUnionUserInfo(queryAdminDTO));
        return new BaseRsp(ResultEnum.SUCCESS, immutableMap);
    }


    @GetMapping("/anchor_apply")
    public BaseRsp listUnionAnchorApply(UnionAnchorApplyQueryAdminDTO queryAdminDTO){
        return unionService.listUnionAnchorApply(queryAdminDTO);
    }




}
